
package com.phevos.common.dbpool;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public abstract class BaseDAO
{

    
    protected static Logger logger = LoggerFactory.getLogger(BaseDAO.class.getName());

    /**
     * ����SQL���ʱ��StringBuffer�Ĵ�С
     */
    protected static final int DAO_SQLBUF_LEN = 1000;

    /**
     * ��ѯ�������:����
     */
    public static final int ORDER_DESC = 1;

    /**
     * ��ݿ�l��
     */
    protected Connection conn = null;

    /**
     * ��Ӧ�����
     */
    protected String tableName = "";

    /**
     * �����ַ�1,����sql����ƥ��
     */
    protected final static String _MATCHSQLSTR_1 = ",) values";

    /**
     * �����ַ�2,����sql����ƥ��
     */
    protected final static String _MATCHSQLSTR_2 = ",)";

    /**
     * �����ַ�3,����sql����ƥ��
     */
    protected final static String _MATCHSQLSTR_3 = ", WHERE";

    /**
     * �����ַ�4,����sql����ƥ��
     */
    protected final static String _MATCHSQLSTR_4 = "SET  WHERE";

    
    private boolean isConnForeign = false;


    protected synchronized void closeConnection()
    {
        if (!isConnForeign)
        {
            // if(conn!=null){
            try
            {
                BasicConnectionPool.getInstance().returnConnection(conn);
            }
            catch (SQLException e)
            {
                logger.error("�黹conn��pool��̳��", e);
            }
            // }
        }
    }

    /**
     * ��ȡl�� ֻ�е�l�Ӳ����ⲿ���������£��Żᴴ��l��;�������κβ���
     */
    protected synchronized void getConnection() throws SQLException
    {
        if (!isConnForeign)
        {
            if (conn == null || conn.isClosed())
            {
                conn = null;// ����l��,δʵ��
                try
                {
                    conn = BasicConnectionPool.getInstance().getConnection();
                }
                catch (Exception e)
                {// DBPool
                    logger.error("error occurs when getConnection from BasicConnectionPool.", e);
                    throw new SQLException("l�ӳ��׳�DBPoolException:" + e.getMessage());
                }
            }
        }
        else
        {// l���ⲿ����
        }
    }

    /**
     * �ر�statement
     * 
     * @param stmt
     */
    protected static void closeStatement(Statement stmt)
    {
        try
        {
            if (stmt != null)
                stmt.close();
        }
        catch (SQLException e)
        {
            logger.error("", e);
        }
    }

    /**
     * @param rs
     */
    protected static void closeResultset(ResultSet rs)
    {
        try
        {
            if (rs != null)
                rs.close();
        }
        catch (SQLException e)
        {
            logger.error("", e);
        }
    }

    /**
     * ȱʡ���췽��
     */
    public BaseDAO()
    {
    }

    /**
     * �����4��ݿ�l�Ӵ���DAO
     * 
     * @param conn
     */
    public BaseDAO(Connection argConn)
    {
        this.conn = argConn;
        this.isConnForeign = true;
    }

    /**
     * ���where�����ȡ�ܵļ�¼��
     */
    public int getCountByWhere(String where) throws SQLException
    {
        int count = 0;
        if (where == null || where.trim().equals(""))
            throw new SQLException("��ѯ�����Ҫ����");
        if (tableName == null || tableName.trim().equals(""))
        {
            throw new SQLException("tableName��Ҫ����");
        }
        StringBuffer sql = new StringBuffer(DAO_SQLBUF_LEN);

        sql.append("SELECT count(0)  FROM " + tableName);
        sql.append(" WHERE ");
        sql.append(where);
        ResultSet rs = null;
        Statement stmt = null;
        getConnection();
        try
        {
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql.toString());
            if (rs.next())
            {
                count = rs.getInt("count(0)");
            }
        }
        catch (SQLException e)
        {
            logger.debug("", e);
            throw e;
        }
        finally
        {
            closeResultset(rs);
            closeStatement(stmt);
            closeConnection();
        }
        return count;
    }

    public void executeUpdate(String sql) throws SQLException
    {
        ResultSet rs = null;
        Statement stmt = null;
        getConnection();
        try
        {
            stmt = conn.createStatement();
            stmt.executeUpdate(sql);
        }
        catch (SQLException e)
        {
            logger.debug("", e);
            throw e;
        }
        finally
        {
            closeResultset(rs);
            closeStatement(stmt);
            closeConnection();
        }
    }
}
